package com.hht.baselibs.adapter

import android.annotation.SuppressLint
import android.view.DragEvent
import android.view.LayoutInflater
import android.view.View
import android.view.View.*
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.hht.baselibs.listener.BaseAdapterItemListener
import com.hht.baselibs.util.ViewUtils

typealias OnBaseItemClickListener<T> = (T) -> Unit


/**
 * Author:chen'hu
 * Time:2023/8/9 9:04
 * Description:This is BaseAdapter
 */
abstract class BaseAdapter<DB : ViewDataBinding, T> :
    Adapter<BaseAdapter<DB, T>.BaseViewHolder>() {

    protected var mLayoutResId: Int = getLayoutResId()

    protected var dataList: List<T> = getAdapterDataList()

    protected var isOnDrag: Boolean = getDrag()

    protected var itemClickListener: BaseAdapterItemListener<T>? = getAdapterItemClickListener()

    protected var mOnBaseItemClickListener: OnBaseItemClickListener<T>? = getOnItemClickListener()

    protected abstract fun getLayoutResId(): Int

    protected abstract fun getAdapterDataList(): List<T>

    protected abstract fun getDrag(): Boolean

    protected abstract fun getAdapterItemClickListener(): BaseAdapterItemListener<T>?

    protected abstract fun getOnItemClickListener(): OnBaseItemClickListener<T>?

    protected abstract fun bindViewHolder(viewDataBinding: DB, t: T)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {

        if (mLayoutResId < 1) {
            throw NullPointerException("layout is not null ...")
        }
        val binding: DB = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context), mLayoutResId, parent, false
        )
        return BaseViewHolder(binding)
    }


    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val itemBean = dataList[position]
        holder.bindViewHolder(itemBean, position)
    }

    override fun getItemCount(): Int {
        return if (dataList.isEmpty()) {
            0
        } else {
            dataList.size
        }
    }


    @SuppressLint("NewApi")
    inner class BaseViewHolder(private val binding: DB) : RecyclerView.ViewHolder(binding.root),
        OnClickListener, OnLongClickListener, OnDragListener {
        private var mItemBean: T? = null
        private var mCurPosition: Int = 0

        init {
            itemView.setOnClickListener(this)
            itemView.setOnLongClickListener(this)
            itemView.setOnDragListener(this)
        }

        fun bindViewHolder(bean: T, position: Int) {
            mItemBean = bean
            mCurPosition = position
            bindViewHolder(binding, bean)
            mItemBean?.let {
                itemClickListener?.onConvertView(ViewUtils.create(itemView), it)
            }
            binding.executePendingBindings()
        }

        override fun onClick(v: View?) {
            mItemBean?.let {
                itemClickListener?.onItemClick(it)
                mOnBaseItemClickListener?.invoke(it)
            }
        }

        override fun onLongClick(v: View?): Boolean {
            mItemBean?.let {
                itemClickListener?.onItemOnLongClick(v!!, it)
            }
            return true
        }

        override fun onDrag(v: View?, event: DragEvent?): Boolean {
            mItemBean?.let {
                if (!isOnDrag) {
                    if (event?.action == DragEvent.ACTION_DROP) {
                        itemClickListener?.onItemOnDrag(v!!, it, event, mCurPosition)
                    }
                }
            }
            return true
        }
    }
}